 /*
 * UMLSynch - Simple RMI part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.rpc.base;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class TIOHandler {
	private ServerSocket server = null;
	private TConfig config = null;
	private TDebug debug = null;
	private Socket acceptedSocket = null;
	private ObjectInputStream objInStream = null;
	private ObjectOutputStream objOutStream = null;
	private Socket client;

	public TIOHandler(TConfig cfg, IDebug dbg) {
		config = cfg;
		if (config == null) {
			config = new TConfig();
		}
		debug = new TDebug(dbg);
	}
	public boolean ServerConnect() {
		try {
			server = new ServerSocket(config.port, 0, 
					InetAddress.getByName(config.hostname));
			return true;
		} catch (UnknownHostException e) {
			debug.Error("Unknown host: " + config.hostname);
		} catch (IOException e) {
			debug.Error("IO exception: " + config.hostname + " exception:" + e.getLocalizedMessage());
		}
		return false;
	}
	
	public boolean conect() {
			try {
				client = new Socket(config.hostname, config.port);
				objInStream = new ObjectInputStream(client.getInputStream());
				objOutStream = new ObjectOutputStream(client.getOutputStream());
				return true;

			} catch (UnknownHostException e) {
				debug.Error(e.getLocalizedMessage());
			} catch (IOException e) {
				debug.Error(e.getLocalizedMessage());			
			}
			return false;
	}

	public boolean connect() {
		try {
			client = new Socket(config.hostname, config.port);
			debug.Error("connected ---- 0 ");
			objOutStream = new ObjectOutputStream(client.getOutputStream());
			debug.Error("connected ----");
			return true;

		} catch (UnknownHostException e) {
			debug.Error(e.getLocalizedMessage());
		} catch (IOException e) {
			debug.Error(e.getLocalizedMessage());			
		}
		return false;
}

	
	public boolean accept() {
		try {
			client = server.accept();
			if (!client.getInetAddress().equals(InetAddress.getByName(config.hostname))) {
				// IF someone tries to accept socket via remote access
				client.close();
				return false;
			}
			return true;
		} catch (IOException e) {
			debug.Error("IO exception on socket accpet " + e.getLocalizedMessage());
		}
		return false;
	}
	
	public ObjectInputStream getObjectInputStream() {
		if (objInStream == null) {
			try {
				objInStream = new ObjectInputStream(client.getInputStream());
			} catch (IOException e) {
				debug.Error(e.getLocalizedMessage());
			}
		}		
		return objInStream;
	}

	public ObjectOutputStream getObjectOutputStream() {
		if (objOutStream == null) {
			try {
				objOutStream = new ObjectOutputStream(client.getOutputStream());
			} catch (IOException e) {
				debug.Error(e.getLocalizedMessage());
			}
		}
		return objOutStream;
	}
	
	public String readString() {
		String data = null;
		try {
			data = (String)getObjectInputStream().readObject();
		} catch (IOException e) {
			debug.Error("readString IOException");
		} catch (ClassNotFoundException e) {
			debug.Error("readString Class not found exception");
		}
		return data;
	}
	public void close() {
		if (client != null) {
			try {
				client.close();
			} catch (IOException e) {
				debug.Error("Exception on client socket close");
			}
		}
		
		objOutStream = null;
		objInStream = null;
	}
}
